<template>
    <div>
        <div class="crop-demo">
            <img :src="cropImg" class="pre-img" v-if="cropImg" />
            <div class="crop-demo-btn" :style="cropStyle">
                <input class="crop-input" :style="cropStyle" type="file" name="image" accept="image/*" @change="setImage" />
                <i class="i-plus">+</i>
            </div>
        </div>

        <el-dialog title="裁剪图片" :append-to-body="append" :close-on-click-modal= "false" :visible.sync="dialogVisible" width="30%">
            <vue-cropper
                ref="cropper"
                :src="imgSrc"
                :aspectRatio = "aspectRatio"
                :ready="cropImage"
                :zoom="cropImage"
                :cropmove="cropImage"
                style="width:100%;height:300px;"
            ></vue-cropper>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelCrop">取 消</el-button>
                <el-button type="primary" @click="handleUpload">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import VueCropper from 'vue-cropperjs'
import 'cropperjs/dist/cropper.css'
// import { upload } from '@/api/upload'
export default {
    name: 'upload',
    components: { VueCropper },
    props: {
        type: {
            type: String,
            default: ''
        },
        aspectRatio: { 
            type: Number,
            default: 1 / 1
        },
        defaultImage: { 
            type: String, 
            default: require('@/assets/image/image.png')
        }

    },
    data: function() {
        return {
            defaultUrl: require('@/assets/image/image.png'),
            append:true,
            file: {},
            imgSrc: '',
            cropImg: '',
            dialogVisible: false,
            cropStyle: {
                width: "90px",
                height: "160px",
                'line-height': "160px",
            },
        };
    },
    components: {
        VueCropper
    },
    created() {
        this.cropImg = ''
        console.log(this.aspectRatio)
        if(this.aspectRatio>1) {
            this.cropStyle.width = 120 * this.aspectRatio + "px"
            this.cropStyle.height = "120px"
            this.cropStyle.lineHeight = "120px"
        } else {
            this.cropStyle.width = 160 * this.aspectRatio * 1.5 + "px"
            this.cropStyle.height = "160px"
            this.cropStyle.lineHeight = "160px"
        }
        
    },
    watch: {
        defaultImage: {
            deep: true,
            handler(val) {
                this.handleSetPreview(val)
            }
        }
    },
    methods: {
        setImage(e) {
            this.file = e.target.files[0];
            if (!this.file.type.includes('image/')) {
                return;
            }
            const reader = new FileReader();
            reader.onload = event => {
                this.dialogVisible = true;
                this.imgSrc = event.target.result;
                this.$refs.cropper && this.$refs.cropper.replace(event.target.result);
            };
            reader.readAsDataURL(this.file);
        },
        cropImage() {
            this.cropImg = this.$refs.cropper.getCroppedCanvas().toDataURL();
        },
        cancelCrop() {
            this.dialogVisible = false;
            this.cropImg = this.defalutImage;
        },
        handleUpload() {
            let self = this
            var data = new FormData()
            data.append('file', this.file)
            data.append('type', this.type)
            upload(data).then(response => {
                if (response.data) {
                    this.dialogVisible = false
                    this.$emit('upload-success',response.data)
                }
            })
        },
        handleError() {
            this.$notify.error({
                title: '上传失败',
                message: '图片上传接口上传失败，可更改为自己的服务器接口'
            });
        },
        handleSetPreview(val) {
            this.cropImg  =  val ? val : this.defaultUrl
        }
    }
};
</script>

<style lang="scss">
.content-title {
    font-weight: 400;
    line-height: 50px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
}
.pre-img {
    width: 100px;
    height: 100px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
}
.crop-demo {
    display: flex;
    align-items: flex-end;
}
.crop-demo-btn {
    position: relative;
    width: 240px;
    height: 135px;
    line-height: 135px;
    padding: 0 20px;
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    color: #fff;
    font-size: 14px;
    border-radius: 5px;
    box-sizing: border-box;
    text-align: center;
    .i-plus{
        font-size: 40px;
        color: #8c939d;
        /* left: 41%; */
        /* font-weight: bold; */
    }
}
.crop-input {
    position: absolute;
    width: 240px;
    height: 135px;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
}


</style>
